package travel.region;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import oracle.jdbc.proxy.annotation.Pre;

import travel.Travel_Information_DAO;
import travel.Travel_Information_VO;

public class Region_DAO implements Region_IDAO {
	Travel_Information_DAO tid = new Travel_Information_DAO();
	@Override//지역 넣기
	public List<Travel_Information_VO> selectGiyic() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		List<Travel_Information_VO> result = new ArrayList<>();
		try{
			conn = tid.openDatabase();
			pstmt = conn.prepareStatement(tid.giyicSQL);
			rs = pstmt.executeQuery();
			while(rs.next()){
				Travel_Information_VO ti = new Travel_Information_VO();
				ti.setTravel_place(rs.getString(1));
				ti.setTravel_duration(rs.getString(2));
				ti.setTravel_price(rs.getString(3));
				ti.setTravel_introdation(rs.getString(4));
				ti.setTravel_notice(rs.getString(5));
				ti.setTravel_votes(rs.getInt(6));
				result.add(ti);
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}finally{tid.close(conn, pstmt, rs);}
		
		return result;
	}
	@Override
	public void votesUpdate(Travel_Information_VO tiv) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		
		try{
			conn = tid.openDatabase();
			pstmt = conn.prepareStatement(tid.votesSQL);
				pstmt.setString(1, tiv.getTravel_place());
//				pstmt.setString(2, tiv.getTravel_votes()+"");
				int cut = pstmt.executeUpdate();
				if(cut == 0 ) System.out.println("안되.~");
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			tid.close(conn, pstmt, null);
		}
	}
	@Override
	public List<Travel_Information_VO> votesImage() {
		Connection conn =null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		List<Travel_Information_VO> result = new ArrayList<>();
		try{
			conn = tid.openDatabase();
			pstmt = conn.prepareStatement(tid.votesIMSQL);
			rs = pstmt.executeQuery();
			while(rs.next()){
			Travel_Information_VO tiv = new Travel_Information_VO();
			tiv.setTravel_image_name(rs.getString(1));
			tiv.setTravel_votes(rs.getInt(2));
			tiv.setRegion_name(rs.getString(3));
			tiv.setTravel_place(rs.getString(4));
			result.add(tiv);
			}
			
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			tid.close(conn, pstmt, rs);
		}
		return result;
	}
	
}
